﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MH3GAPP.Bll;
using MH3GAPP.Dal;
using MH3GAPP.Common;

namespace MH3GAPP.Frm
{
    public partial class ArmourForm : Form
    {
        #region var_全局变量
        private static ArmourForm instance;
        private MH3GBll mh3gBll = new MH3GBll();
        DataTable mainData = new DataTable();
        bool rareIsChange = false;
        #endregion


        #region Constructor_构造函数
        private ArmourForm()
        {
            InitializeComponent();
        }
        #endregion

        #region GetInstance_取得实例
        public static ArmourForm GetInstance()
        {
            if (instance == null)
            {
                instance = new ArmourForm();
            }
            return instance;
        }
        #endregion

        #region ArmourForm_FormClosing_窗口关闭
        private void ArmourForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            instance = null;
        }
        #endregion

        #region Form_Load
        private void ArmourForm_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;
            rareIsChange = true;

            mainData = getArmDisplayDate(searchTextBox.Text);

            setMainGridData();
        }
        #endregion

        #region getArmDisplayDate_取得数据
        private DataTable getArmDisplayDate(string serchStr)
        {
            DataTable dt = new DataTable();

            dt = mh3gBll.getArmDisplayDate(
                serchStr,
                checkBox1.Checked,
                checkBox2.Checked,
                checkBox3.Checked,
                checkBox4.Checked,
                checkBox5.Checked,
                checkBox6.Checked,
                checkBox7.Checked,
                checkBox8.Checked,
                checkBox9.Checked,
                comboBox1.SelectedIndex);

            return dt;
        }
        #endregion

        #region setMainGridData_设置列表
        private void setMainGridData()
        {
            mainGridView.Rows.Clear();
            mainGridView.SetDoubleBuffered(true);
            //DoubleBuffer.SetDoubleBuffered(mainGridView, true);

            foreach (DataRow tmpRow in mainData.Rows)
            {
                int tmpp = int.Parse(tmpRow["Amr_ID"].ToString());

                if (tmpp == -1)
                {
                    continue;
                }

                DataGridViewRow row = new DataGridViewRow();
                string icoName = "";
                //编号
                DataGridViewTextBoxCell cellNO = new DataGridViewTextBoxCell();
                cellNO.Value = tmpRow["Amr_ID"];
                row.Cells.Add(cellNO);
                //做成
                DataGridViewCheckBoxCell cellDone = new DataGridViewCheckBoxCell();
                cellDone.Value = tmpRow["Done"];
                row.Cells.Add(cellDone);
                //位置
                DataGridViewImageCell cellPart = new DataGridViewImageCell();
                icoName = @"_" + tmpRow["Amr_Rare"].ToString() + tmpRow["Amr_Part"].ToString();
                cellPart.Value = MH3GAPP.Properties.Resources.ResourceManager.GetObject(icoName);
                row.Cells.Add(cellPart);
                //中文名称
                DataGridViewTextBoxCell cellName = new DataGridViewTextBoxCell();
                cellName.Value = tmpRow["Amr_Name_1"];
                row.Cells.Add(cellName);
                //日文名称
                DataGridViewTextBoxCell cellNameJP = new DataGridViewTextBoxCell();
                cellNameJP.Value = tmpRow["Amr_Name_3"];
                row.Cells.Add(cellNameJP);
                //远近
                DataGridViewImageCell cellBG = new DataGridViewImageCell();
                icoName = @"BG" + tmpRow["Amr_BG"].ToString();
                cellBG.Value = MH3GAPP.Properties.Resources.ResourceManager.GetObject(icoName);
                row.Cells.Add(cellBG);
                //男女
                DataGridViewImageCell cellFM = new DataGridViewImageCell();
                icoName = @"MF" + tmpRow["Amr_MF"].ToString();
                cellFM.Value = MH3GAPP.Properties.Resources.ResourceManager.GetObject(icoName);
                row.Cells.Add(cellFM);
                //稀有度
                DataGridViewTextBoxCell cellRare = new DataGridViewTextBoxCell();
                cellRare.Value = tmpRow["Amr_Rare"];
                row.Cells.Add(cellRare);
                //洞数
                DataGridViewTextBoxCell cellSlot = new DataGridViewTextBoxCell();
                cellSlot.Value = tmpRow["Amr_Slot"];
                row.Cells.Add(cellSlot);
//                 //初始防御
//                 DataGridViewTextBoxCell cellDef = new DataGridViewTextBoxCell();
//                 cellDef.Value = tmpRow["Def"];
//                 row.Cells.Add(cellDef);
                //最大防御
                DataGridViewTextBoxCell cellMax = new DataGridViewTextBoxCell();
                cellMax.Value = tmpRow["Def_Max"];
                row.Cells.Add(cellMax);
//                 //火抗
//                 DataGridViewTextBoxCell cellFire = new DataGridViewTextBoxCell();
//                 cellFire.Value = tmpRow["Res_Fire"];
//                 row.Cells.Add(cellFire);
//                 //水抗
//                 DataGridViewTextBoxCell cellWater = new DataGridViewTextBoxCell();
//                 cellWater.Value = tmpRow["Res_Water"];
//                 row.Cells.Add(cellWater);
//                 //冰抗
//                 DataGridViewTextBoxCell cellIce = new DataGridViewTextBoxCell();
//                 cellIce.Value = tmpRow["Res_Ice"];
//                 row.Cells.Add(cellIce);
//                 //雷抗
//                 DataGridViewTextBoxCell cellThunder = new DataGridViewTextBoxCell();
//                 cellThunder.Value = tmpRow["Res_Thunder"];
//                 row.Cells.Add(cellThunder);
//                 //龙抗
//                 DataGridViewTextBoxCell cellDragon = new DataGridViewTextBoxCell();
//                 cellDragon.Value = tmpRow["Res_Dragon"];
//                 row.Cells.Add(cellDragon);
//                 //生产价
//                 DataGridViewTextBoxCell cellPrice = new DataGridViewTextBoxCell();
//                 cellPrice.Value = tmpRow["Price"];
//                 row.Cells.Add(cellPrice);

                mainGridView.Rows.Add(row);

                groupBox1.Text = "防具列表 : " + mainGridView.RowCount.ToString() + "/1651";
            }
        }
        #endregion

        #region mainGridView_CellValueChanged_是否做成cell改变
        private void mainGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex == 1)
            {
                try
                {
                    bool value = (bool)mainGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                    int amrID = int.Parse(mainData.Rows[e.RowIndex + 1]["Amr_ID"].ToString());

                    mh3gBll.changeDone(amrID, value);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
        #endregion

        #region mainGridView_SelectionChanged_列表选中列改变
        private void mainGridView_SelectionChanged(object sender, EventArgs e)
        {
        }
        #endregion

        #region searchbutton_Click_开始查询
        private void searchbutton_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt = getArmDisplayDate(searchTextBox.Text);
            int amrID = int.Parse(dt.Rows[0]["Amr_ID"].ToString());

            foreach (DataGridViewRow selRow in mainGridView.Rows)
            {
                int index = int.Parse(selRow.Cells[0].Value.ToString());

                if (index == amrID)
                {
                    selRow.Selected = true;
                    mainGridView.CurrentCell = selRow.Cells[0];
                    break;
                }
            }
        }
        #endregion

        #region checkBox_CheckedChanged_查询条件改变
        private void checkBox_CheckedChanged(object sender, EventArgs e)
        {
            mainData.Clear();
            mainData = getArmDisplayDate("");

            setMainGridData();
        }
        #endregion

        #region comboBox1_SelectedIndexChanged_查询装备等级改变
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rareIsChange)
            {
                mainData.Clear();
                mainData = getArmDisplayDate("");

                setMainGridData();
            }
        }
        #endregion

    }
}
